package cn.gzsxt.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import cn.gzsxt.dao.SysUserDao;
import cn.gzsxt.po.SysUser;
import cn.gzsxt.query.SysUserQueryObject;
import cn.gzsxt.utils.DBUtil;

public class SysUserDaoImpl extends BaseDaoImpl implements SysUserDao {

	@Override
	public SysUser findByUnameAndPwd(String uname, String pwd) {
		String sql = "select * from t_sysuser where uname = ? and pwd = ?";

		return super.selectSingleRow(SysUser.class, sql, uname, pwd);
	}

	@Override
	public SysUser findByUname(String uname) {
		String sql = "select * from t_sysuser where uname = ?";
		return super.selectSingleRow(SysUser.class, sql, uname);
	}

	@Override
	public int insert(SysUser user) {
		String sql = "insert into t_sysuser (uname,nickname,pwd,phone,email,qq,regtime)values(?,?,?,?,?,?,?)";

		Object[] params = { user.getUname(), user.getNickname(), user.getPwd(),
				user.getPhone(), user.getEmail(), user.getQq(),
				user.getRegtime() };

		int row = super.update(sql, params);
		return row;
	}

	@Override
	public List<SysUser> findByCondition(SysUserQueryObject qo) {
		String sql = "select * from t_sysuser where delstatus = 1 limit ?,?";
		// 有条件的情况需要手动拼接条件 :关键字搜索用户名和昵称
		if (qo.getKeyword() != null && !"".equals(qo.getKeyword().trim())) {
			sql = "select * from t_sysuser where uname like '%"+qo.getKeyword()+"%' or nickname like '%"+qo.getKeyword()+"%'  limit ?,?";
		} 
		return super.selectListRow(SysUser.class, sql,qo.getBeginIdex(),qo.getPageSize());

	}
	@Override
	public Integer findTotalCountByCondition(SysUserQueryObject qo) {
		String sql = "select count(*) from t_sysuser where delstatus = 1";
		// 有条件的情况需要手动拼接条件 :关键字搜索用户名和昵称
		if (qo.getKeyword() != null && !"".equals(qo.getKeyword().trim())) {
			sql = "select count(*) from t_sysuser where uname like '%"+qo.getKeyword()+"%' or nickname like '%"+qo.getKeyword()+"%'";
		}
		return super.selectTotalCount(sql);
	}

	@Override
	public int deleteById(Integer id) {
		//真删除:有些特殊的系统是不能真删除的
		//String sql = "delete from t_sysuser where id = ?";
		//假删除:就是改变数据的状态,改变状态以后再查询的时候不让显示即可,这样有利于后期程序项目出现问题进行数据的追踪
		String sql = "update t_sysuser set delstatus = 0 where id = ?";
		return super.update(sql, id);
	}

	@Override
	public int updateSysUser(SysUser u) {
		String sql = "update t_sysuser set nickname = ? ,pwd = ?,phone=?,email=?,qq=?,regtime=? where id = ?";
		Object[] params = {u.getNickname(),u.getPwd(),u.getPhone(),u.getEmail(),u.getQq(),u.getRegtime(),u.getId()};
		return super.update(sql, params);
	}

}
